package wallet

import (
	"context"
	"go.uber.org/zap"
	gamePb "navi-mumbai/app/rpc/game/pb"
	"navi-mumbai/app/rpc/wallet/pb"
	"navi-mumbai/common/key/dbkey"
	"time"

	"navi-mumbai/app/api/business/internal/svc"
	"navi-mumbai/app/api/business/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRefreshOneGameWalletLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserRefreshOneGameWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRefreshOneGameWalletLogic {
	return &UserRefreshOneGameWalletLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserRefreshOneGameWalletLogic) UserRefreshOneGameWallet(req *types.UserRefreshOneGameWalletReq) (resp *types.UserRefreshOneGameWalletResp, err error) {
	var in gamePb.GetGameWalletBalanceReq
	in.Uid = req.Uid
	in.ProviderId = req.ProviderId
	in.MoneyType = req.MoneyType
	var rsp *gamePb.GetGameWalletBalanceResp
	rsp, err = l.svcCtx.GameWalletRpc.GetGameWalletBalance(l.ctx, &in)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return
	}
	//
	var in2 pb.UpdateGameWalletBalanceReq
	in2.Uid = req.Uid
	in2.ProviderId = req.ProviderId
	in2.MoneyType = req.MoneyType
	in2.MoneyAmount = rsp.Balance
	_, err = l.svcCtx.WalletUsersRpc.UpdateGameWalletBalance(l.ctx, &in2)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return
	}
	resp = &types.UserRefreshOneGameWalletResp{
		ProviderId: req.ProviderId,
		MoneyType:  req.MoneyType,
		Balance:    rsp.Balance,
		UpdateTime: time.Now().Format(dbkey.DateTimeCNFormat),
	}
	return
}
